<template>
	<view class="">
		<!-- #ifdef MP-WEIXIN -->
		<camera id="myCamera" device-position="back" flash="auto" bindcameraframe="onCameraFrame">
		<!-- #endif -->
			<view :class="['page','scan-code-page',mpweixin&&'mpweixin']">
				<view class="back" @tap="goBack()">
					<svg-icon icon="arrowLeft-white"></svg-icon>
				</view>
				<view class="scan-content">
					<loading-mask v-model="loading">
						<view class="outer-layer">
							<view class="inner-layer">
								<!-- #ifndef MP-WEIXIN -->
								<view class="text">
									请选择相册二维码
								</view>
								<!-- #endif -->
								<canvas canvas-id="myCanvas" style="width: 100%; height: 100%;"></canvas>
								<view class="line">
								</view>
							</view>
						</view>
					</loading-mask>
					<view class="text">
						请对准二维码系统自动扫描
					</view>
				</view>
				<view class="footer">
					<view class="item active">
						相机
					</view>
					<view class="item picture" @tap="chooseImage">
						图库
					</view>
				</view>
			</view>
		<!-- #ifdef MP-WEIXIN -->
		</camera>
		<!-- #endif -->
	</view>
</template>

<script>
	export default {
		data() {
			return {
				loading: false,
				mpweixin: false
			}
		},
		onLoad() {
			// #ifdef MP-WEIXIN
			this.mpweixin = true
			uni.scanCode({
				// scanType: ['QR_CODE'], //条形码
				success: function(res) {
					// console.log('条码类型：' + res.scanType);
					// console.log('条码内容：' + res.result);
					// 微信小程序
					if (res.errMsg == "scanCode:ok") {
						// 扫描到的信息
						console.log(res.result);
					} else {
						console.log("未识别到二维码，请重新尝试！")
					}
				}
			});
			// #endif
		},
		methods: {
			onCameraFrame(e) {
				const ctx = uni.createCanvasContext('myCanvas', this);
				const frame = e.detail.frame;
				ctx.drawImage(frame, 0, 0, 300, 300);
				ctx.draw();
			},
			chooseImage() {
				const self = this
				this.loading = true;
				uni.chooseImage({
					count: 1, // 选择一张图片
					success: async (chooseImageRes) => {
						const tempFilePaths = chooseImageRes.tempFilePaths;
						self.loading = false;
						/* try {
							const uploadRes = await self.uploadImage(tempFilePaths[0]);
							const jsonObject = JSON.parse(uploadRes);
							self.workNumber.headImage = jsonObject.data.originUrl
						} catch (error) {
							console.error('上传失败：', error);
						} finally {
							self.loading = false;
						} */
					},
					error: async () => {
						self.loading = false;
					}
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
	$qr-code-size: 300px;

	@keyframes move-up-down {
		0% {
			top: 0;
		}

		100% {
			top: $qr-code-size;
		}
	}

	#myCamera {
		width: 100vw;
		height: 100vh;
	}

	.scan-code-page {
		background-color: rgba(0, 0, 0, 0.9) !important;
		height: 100vh;
		padding: 15px;
		box-sizing: border-box;

		&.mpweixin {
			background-color: rgba(0, 0, 0, 0.5) !important;
		}

		.back {
			background-color: rgba(255, 255, 255, 0.2);
			width: 60rpx;
			height: 60rpx;
			border-radius: 50%;
			display: flex;
			align-items: center;
			justify-content: center;
		}

		.scan-content {
			margin: 0 auto;
			margin-top: 45%;
			display: flex;
			flex-direction: column;
			align-items: center;
			gap: 20px;

			.text {
				color: $white;
			}

			.outer-layer {
				width: $qr-code-size;
				height: $qr-code-size;
				background-image: url(https://file.dgyxy.com/resource/icon/qrcode/line-square.svg);
				background-repeat: no-repeat;
				background-size: 100% 100%;

				display: flex;
				align-items: center;
				justify-content: center;

				.inner-layer {
					width: 100%;
					height: 100%;
					margin: 0 auto;
					background-color: rgba(255, 255, 255, 0.1);
					position: relative;
					display: flex;
					align-items: center;
					justify-content: center;

					.text {
						width: 100%;
						text-align: center;
					}

					.line {
						position: absolute;
						width: 100%;
						height: 2px;
						background-image: url(https://file.dgyxy.com/resource/icon/qrcode/line-horizontal.svg);
						background-repeat: no-repeat;
						background-size: 100% 100%;
						animation: move-up-down 2.5s ease-in-out infinite;
					}
				}
			}
		}

		.footer {
			position: fixed;
			bottom: 0;
			left: 0;
			width: 100%;
			color: white;
			display: flex;
			align-items: center;
			justify-content: space-around;
			padding: 20px 0;

			.item {
				flex: 1;
				text-align: center;

				&.active {
					color: $brand-500;
				}
			}
		}
	}
</style>